//
//  EmojiManager.swift
//  1-自定义键盘搭建
//
//  Created by yellow on 15/10/23.
//  Copyright © 2015年 yellow. All rights reserved.
//

import UIKit


/// 一个针对于emoji表情图标管理的单例
class EmojiManager {

    
//    修改为单例可以避免每次使用表情包数据时都要从磁盘重新加载一堆 plist 文件
    static let shareManager:EmojiManager = EmojiManager()
    
    /// 存放表情类型集合
    lazy var emojiPackages:[EmojiPackage] = [EmojiPackage]()
    
    //设置初始化方法为private，是防止外部使用者通过EmojiManager()来创建对象
    private init(){
        
        //添加最近分组
        emojiPackages.append(EmojiPackage(dict: ["group_name_cn":"最近"]))
        
        //获取emoticons.plist
        let path = NSBundle.mainBundle().pathForResource("emoticons.plist", ofType: nil, inDirectory: "Emoticons.bundle")!
        
        let emoticonsPlistDict = NSDictionary(contentsOfFile: path)
        
        let emojiPackageArray = emoticonsPlistDict!["packages"] as? [[String:AnyObject]]
        
        //获取packages数组中所有key为id的值，并组成数组
        let idArray = (emojiPackageArray as! NSArray).valueForKey("id") as! [String]
        
        //加载各个id对应的info.plist
        for id in idArray{
            loadInfoPlistById(id)
        }
        
    }
    
    /// 根据传来的id，当成子路径，从而来获取3个不同文件夹的plist
    ///
    /// - parameter id:子路径
    private func loadInfoPlistById(id:String){
        //拼接path
        let path = NSBundle.mainBundle().pathForResource("info.plist", ofType: nil, inDirectory: "Emoticons.bundle/\(id)")!
        
        if let singlePlistDict = NSDictionary(contentsOfFile: path) as? [String:AnyObject]{
            //读取每个info.plist，生成EmojiPackage，再存入emojiPackages中
            emojiPackages.append(EmojiPackage(dict: singlePlistDict))
        }
    }
}
